/**  
 * @Title: UserController.java
 * @Package com.xzit.tmms.controller.user
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月22日 下午9:14:08
 * @version V1.0  
 */
package com.xzit.tmms.controller.user;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xzit.tmms.bean.user.User;
import com.xzit.tmms.common.base.BaseController;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.service.user.RoleService;
import com.xzit.tmms.service.user.UserService;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月22日 下午9:14:08
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Resource
	private UserService userService;

	@Resource
	private RoleService roleService;

	@RequestMapping("")
	public String index() {
		return "user/user-index";
	}

	@RequestMapping("/addUser")
	public String addUser(Model model) throws ServiceException {
		model.addAttribute("roleList", roleService.getRoleList());
		return "user/user-add";
	}

	@RequestMapping("/addUserSubmit")
	public String addUserSubmit(User user) throws ServiceException {
		userService.addUser(user);
		return "redirect:/user.action";
	}

	@RequestMapping("/queryUser")
	public String queryUser(Model model) throws ServiceException {
		Page<Map<String, Object>> page = new Page<>(getPageIndex(), getPageSize());
		model.addAttribute("page", userService.queryUser(page));
		return "user/user-query";
	}

	@RequestMapping("/deleteUser")
	public String deleteUser(Integer userId) throws ServiceException {
		userService.deleteUser(userId);
		return "forward:queryUser.action";
	}

	@RequestMapping("/updateUser")
	public String update(Model model, Integer userId) throws ServiceException {
		User user = userService.getUserById(userId);
		model.addAttribute("user", user);
		model.addAttribute("roleList", roleService.getRoleList());
		return "user/user-update";
	}

	@RequestMapping("/updateUserSubmit")
	public String updateUserSubmit(Model model, User user) throws ServiceException {
		userService.updateUser(user);
		return "redirect:/user.action";
	}
}
